package com.chx.service.Impl;

import com.chx.constant.StatusConstant;
import com.chx.context.BaseContext;
import com.chx.dto.StockInOrderDetailsDto;
import com.chx.dto.StockInOrderDto;
import com.chx.dto.StockInOrderPageQueryDto;
import com.chx.entity.Products;
import com.chx.entity.StockInOrderDetails;
import com.chx.entity.StockInOrders;
import com.chx.entity.StockLogs;
import com.chx.mapper.*;
import com.chx.result.PageResult;
import com.chx.service.StockInOrderService;
import com.chx.vo.StockInOrderDetailsVo;
import com.chx.vo.StockInOrderVo;
import com.chx.wrapper.StockInOrderWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author cp-月入过万
 * @Date 2025/5/31
 */
@Service
public class StockInOrderServiceImpl implements StockInOrderService {
    @Autowired
    private StockInOrderMapper stockInOrderMapper;
    @Autowired
    private StockInOrderDetailMapper stockInOrderDetailMapper;

    @Autowired
    private StockLogMapper stockLogMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Transactional
    @Override
    public void save(StockInOrderDto stockInOrderDto) {
        StockInOrders stockInOrders = new StockInOrders();
        BeanUtils.copyProperties(stockInOrderDto, stockInOrders);
        //1、订单类型为其他入库
        stockInOrders.setRelatedOrderType(StatusConstant.ENABLE);
        //2、总金额
        Double  totalAmount = 0.0;
        for(StockInOrderDetailsDto stockInOrderDetailsDto:stockInOrderDto.getStockInOrderDetailsDtoList())
            totalAmount += stockInOrderDetailsDto.getSubtotal();
        stockInOrders.setTotalAmount(totalAmount);
        stockInOrderMapper.insert(stockInOrders);

        StockInOrderDetails  stockInOrderDetails = new StockInOrderDetails();
        stockInOrderDetails.setOrderId(stockInOrders.getOrderId());
        for(StockInOrderDetailsDto stockInOrderDetailsDto:stockInOrderDto.getStockInOrderDetailsDtoList()) {
            BeanUtils.copyProperties(stockInOrderDetailsDto, stockInOrderDetails);
            stockInOrderDetailMapper.insert(stockInOrderDetails);
        }
    }

    @Transactional
    @Override
    public void update(StockInOrderDto stockInOrderDto) {
        StockInOrders stockInOrders = new StockInOrders();
        BeanUtils.copyProperties(stockInOrderDto, stockInOrders);
        Double  totalAmount = 0.0;
        for(StockInOrderDetailsDto stockInOrderDetailsDto:stockInOrderDto.getStockInOrderDetailsDtoList())
            totalAmount += stockInOrderDetailsDto.getSubtotal();
        stockInOrders.setTotalAmount(totalAmount);
        stockInOrderMapper.update(stockInOrders);

        stockInOrderDetailMapper.delete(stockInOrderDto.getOrderId());
        StockInOrderDetails  stockInOrderDetails = new StockInOrderDetails();
        stockInOrderDetails.setOrderId(stockInOrders.getOrderId());
        for(StockInOrderDetailsDto stockInOrderDetailsDto:stockInOrderDto.getStockInOrderDetailsDtoList()) {
            BeanUtils.copyProperties(stockInOrderDetailsDto, stockInOrderDetails);
            stockInOrderDetailMapper.insert(stockInOrderDetails);
        }
    }

    @Override
    public void updateStatus(Long id) {
        StockInOrders stockinorders = new StockInOrders();
        stockinorders.setOrderId(id);
        stockinorders.setStatus(StatusConstant.ENABLE);
        stockInOrderMapper.update(stockinorders);
    }

    @Override
    public StockInOrderVo getById(Long id) {
        StockInOrders stockInOrders = stockInOrderMapper.getById(id);
        List<StockInOrderDetails>  stockInOrderDetails = stockInOrderDetailMapper.getByOrderId(id);
        StockInOrderVo stockInOrderVo = new StockInOrderVo();
        BeanUtils.copyProperties(stockInOrders, stockInOrderVo);
        stockInOrderVo.setUserName(userMapper.getById(stockInOrders.getUserId()).getUsername());
        List<StockInOrderDetailsVo>  stockInOrderDetailsVos = new ArrayList<>();
        for(StockInOrderDetails stockInOrderDetail:stockInOrderDetails){
            StockInOrderDetailsVo stockInOrderDetailsVo = new StockInOrderDetailsVo();
            BeanUtils.copyProperties(stockInOrderDetail, stockInOrderDetailsVo);
            stockInOrderDetailsVo.setProductName(productMapper.getById(stockInOrderDetail.getProductId()).getProductName());
            stockInOrderDetailsVos.add(stockInOrderDetailsVo);
        }
        stockInOrderVo.setStockInOrderDetailsVoList(stockInOrderDetailsVos);
        return stockInOrderVo;
    }

    @Override
    public PageResult page(StockInOrderPageQueryDto stockInOrderPageQueryDto) {
        PageHelper.startPage(stockInOrderPageQueryDto.getPage(), stockInOrderPageQueryDto.getPageSize());
        Page<StockInOrders> page = stockInOrderMapper.page(stockInOrderPageQueryDto);
        long total = page.getTotal();
        List<StockInOrders> records = page.getResult();
        List<StockInOrderWrapper> stockInOrderWrappers = records.stream().map(record ->{
             String userName = userMapper.getById(record.getUserId()).getUsername();
             return new StockInOrderWrapper(record, userName);
        }).collect(Collectors.toList());
        return new PageResult(total, stockInOrderWrappers);
    }

    @Transactional
    @Override
    public void commit(Long id) {
        StockInOrders stockinorders = new StockInOrders();
        stockinorders.setOrderId(id);
        stockinorders.setStatus(StatusConstant.ROLE_USER);
        stockInOrderMapper.update(stockinorders);
        // 日志表插入,还会更改商品的库存数目
        Products products = new Products();
        StockInOrderVo  stockInOrderVo = getById(id);

        StockLogs stockLogs = new StockLogs();
        stockLogs.setChangeType(StatusConstant.DISABLE);
        if(Objects.equals(stockInOrderVo.getRelatedOrderType(), StatusConstant.DISABLE))
            stockLogs.setRelatedOrderType(StatusConstant.DISABLE);
        else
            stockLogs.setRelatedOrderType(StatusConstant.ROLE_USER);
        stockLogs.setRelatedOrderId(id);
        stockLogs.setUserId(stockInOrderVo.getUserId());
        stockLogs.setOperationTime(stockInOrderVo.getOrderDate());
        stockLogs.setRemark(stockInOrderVo.getRemark());
        for(StockInOrderDetailsVo stockInOrderDetailsVo:stockInOrderVo.getStockInOrderDetailsVoList()){
            stockLogs.setProductId(stockInOrderDetailsVo.getProductId());
            stockLogs.setChangeQuantity(stockInOrderDetailsVo.getQuantity());
            stockLogs.setBeforeQuantity(productMapper.getById(stockInOrderDetailsVo.getProductId()).getStockQuantity());
            stockLogs.setAfterQuantity(stockLogs.getBeforeQuantity()+stockLogs.getChangeQuantity());

            products.setProductId(stockInOrderDetailsVo.getProductId());
            products.setStockQuantity(stockLogs.getAfterQuantity());
            productMapper.update(products);
            stockLogMapper.addStockLog(stockLogs);
        }
    }
}
